package com.bifrost.web.controller;

import com.bifrost.annotation.Authorization;
import com.bifrost.annotation.CurrentUser;
import com.bifrost.entity.Customer;
import com.bifrost.entity.CustomerAddress;
import com.bifrost.model.AddressModel;
import com.bifrost.model.CustomerModel;
import com.bifrost.common.model.ResultModel;
import com.bifrost.service.CustomerService;
import com.bifrost.util.TransformUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;

/**
 * @author rangf
 * @date 2017/11/7
 */
@RestController
@Api("个人中心")
@RequestMapping("/api/personal")
public class PersonalController {

    @Autowired
    CustomerService customerService;

    @Authorization
    @ApiOperation("获取用户积分")
    @GetMapping("/bonus")
    public ResultModel getBonus(@CurrentUser Long userId) {
        Customer customer = customerService.findCustomer(userId);
        BigInteger bonus = customerService.getCustomerPoints(customer);
        return ResultModel.ok().put("bonus", bonus);
    }

    @Authorization
    @ApiOperation("返回用户基本信息")
    @GetMapping("/info")
    public ResultModel getCustomerInfo(@CurrentUser Long userId) {
        CustomerModel model = customerService.getCustomerModel(userId);
        return ResultModel.ok().put("info", model);
    }

    /*@Authorization
    @ApiOperation("获取所有收货地址")
    @GetMapping("/addrs")
    public ResultModel addrList(@CurrentUser Customer customer){
        List<AddressModel> addressModelList = new ArrayList<>();
        List<CustomerAddress> addressList = customerService.getAllAddress(customer);
        if(addressList == null || addressList.isEmpty()){
            return ResultModel.ok("没有数据");
        }
        addressList.forEach(customerAddress -> {
            addressModelList.add(TransformUtils.transformToAddressModel(customerAddress));
        });
        return ResultModel.ok().put("addrs",addressModelList);
    }*/
    @Authorization
    @ApiOperation("会员基本信息维护")
    @PostMapping("/info/update")
    public ResultModel updateInfo(@CurrentUser Long userId, CustomerModel model) {
        Customer customer = customerService.findCustomer(userId);
        if(customerService.updateCustomerInfo(customer, model) != null){
            return ResultModel.ok("修改成功");
        }
        return ResultModel.error("修改失败");
    }

    @Authorization
    @ApiOperation("获取收货地址详情")
    @GetMapping("/addr")
    public ResultModel addrDetail(Long addressId) {
        CustomerAddress address = customerService.getAddrDetail(addressId);
        return ResultModel.ok().put("addr", TransformUtils.transformToAddressModel(address));
    }

    @Authorization
    @ApiOperation("新增收货地址")
    @PostMapping("/addr/add")
    @ApiImplicitParam(paramType = "query", name = "addressModel", value = "收货地址Model", required = true, dataType = "String")
    public ResultModel addAddr(@CurrentUser Long userId, AddressModel addressModel) {
        Customer customer = customerService.findCustomer(userId);
        if(customerService.addAddr(customer, addressModel) != null){
            return ResultModel.ok("添加成功");
        }
        return ResultModel.error("添加失败");
    }

    @Authorization
    @ApiOperation("修改更新收货地址")
    @PostMapping("/addr/update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "addressModel", value = "收货地址Model", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "addressId", value = "addressId", required = false, dataType = "String")
    })
    public ResultModel updateAddr(@CurrentUser Long userId, AddressModel addressModel, String addressId) {
        Customer customer = customerService.findCustomer(userId);
        if(customerService.updateAddr(customer, addressModel, addressId) != null){
            return ResultModel.ok("修改成功");
        }
        return ResultModel.error("修改失败");
    }

}
